#include <cstring>
#include <iostream>
#include <queue>
#include <unordered_map>

using namespace std;

const int N = 6;

int n;
string a[N], b[N];

int extend(queue<string>& q, unordered_map<string, int>&da, unordered_map<string, int>& db, string a[], string b[]) {
    for (int k = 0, ts = q.size(); k < ts; ++ k) {
        string t = q.front();
        q.pop();
        
        for (int i = 0; i < t.size(); ++ i) // 遍历该状态时的每个字符
            for (int j = 0; j < n; ++ j)  // 遍历规则                
                if (t.substr(i, a[j].size()) == a[j]) {
                    string state = t.substr(0, i) + b[j] + t.substr(i + a[j].size());
                    if (da.count(state)) continue;
                    if (db.count(state)) return da[t] + 1 + db[state];
                    da[state] = da[t] + 1;
                    q.push(state);
                }
    }
    
    return 11;
}

int bfs(string A, string B) {
    unordered_map<string, int> da, db;
    queue<string> qa, qb;
    
    qa.push(A), da[A] = 0;
    qb.push(B), db[B] = 0;
    
    while (qa.size() && qb.size()) {
        int t = 0;
        if (qa.size() <= qb.size()) t = extend(qa, da, db, a, b);
        else t = extend(qb, db, da, b, a);
        
        if (t <= 10) return t;
    }
    
    return 11;
}

int main() {
    string A, B;
    cin >> A >> B;
    while (cin >> a[n] >> b[n]) n ++ ;
    
    int step = bfs(A, B);
    if (step > 10) puts("NO ANSWER!");
    else printf("%d\n", step);
    
    return 0;
}